<?php
 //封装PDO类
class Mypdo{
    private $allow = array('mysql','oracle');
    private $type;
    private $host;
    private $dbname;
    private $user;
    private $password;
    private $charset;
    private $prefix;
    private $fetch_type;

    //常量
    const MYPDO_FETCH_ASSOC = 2;
    const MYPDO_FETCH_BOTH = 4;
    const MYPDO_FETCH_NUM = 1;
   
    private $pdo; //连接pdo对象
    private $stmt;//保存结果集

    //构造方法
    public function __construct($arr=array()){
        //给变量赋值
        $this->type = isset($arr["type"])?$arr["type"]:"mysql";
        $this->host = isset($arr["host"])?$arr["host"]:"localhost";
        $this->dbname= isset($arr["dbname"])?$arr["dbname"]:"ghp_mysql";
        $this->user = isset($arr["user"])?$arr["user"]:"root";
        $this->password = isset($arr["password"])?$arr["password"]:"";
        $this->charset = isset($arr["charset"])?$arr["charset"]:"utf8";
        $this->prefix = isset($arr["prefix"])?$arr["prefix"]:"pro_";
        $this->fetch_type = isset($arr["fetch_type"])?$arr["fetch_type"]:2;


        if(!in_array($this->type, $this->allow)){
        echo "不支持当前数据库类型".$this->type;
        exit();
        }
        $this->connect();
        $this->setChar();
        $this->setErrorException();
    }

    //设置PDO连接
    private function connect(){
        try{
        // var_dump($this->type);
        // exit();
        $this->pdo = new PDO("{$this->type}:host={$this->host};dbname={$this->dbname}","{$this->user}","{$this->password}");
        // var_dump( $this->pdo);
        // exit();
        }catch(PDOException $e){
              var_dump($e->getTrace());
              exit();
              }
    }

    //设置字符集
    private function setChar(){
        // var_dump($this->charset);
        $sql = "set names {$this->charset}";
        $this->myExec($sql);//设置字符集是不会返回结果的，因此不能用query();
        // var_dump("字符集已被设置");
        // exit();
    }


    //设置获取的格式，是以关联数组获取还是索引数组获取
    public function setMyFecthMode(){
        $this->stmt->setFetchMode($this->fetch_type);
        // var_dump(MYPDO_FETCH_ASSOC);
    }

    //异常处理
    private function setErrorException(){
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }

    //添加增删改查里面的增加方法。
    public function my_insert($sql){
        $this->myExec($sql);
        return $this->pdo->lastInsertId();
    }

    //获取数据
    public function db_getRow($sql){
        //PDO对象执行SQL语句
       $this->stmt = $this->pdo->query($sql);
       $this->setMyFecthMode();
       //获取数据
       return $this->stmt->fetchall();

    }

    //exec的查询错误处理
    private function myExec($sql){
        //使用异常处理
        try{
            $res = $this->pdo->exec($sql);

            //没有错误
            return $res;
        }catch(PDOException $e){
            var_dump($e->getTrace());
            return false;
        }
    }
    
}
    // $arr = array("type"=>"mysql","host"=>"localhost","dbname"=>"ghp_mysql","user"=>"root","password"=>"","charset"=>"utf8","prefix"=>"pro_");
    // $pdo1 = new Mypdo( );
    // var_dump($pdo1);

?>